import { getEmailPage, Deleteemail } from '@/api/emailpage'
import { Message } from 'element-ui'

const state = {
  pageCount: 0,
  emailList: []
}

const mutations = {
  update(state, payload) {
    for (const key in payload) {
      state[key] = payload[key]
    }
  }
}

const actions = {
  async getEmailPage({ commit }, payload = {}) {
    console.log('payload...', payload)
    const { page = 1, pageSize = 12, ...options } = payload
    const result = await getEmailPage(page, options, pageSize)
    console.log('result...', result)
    if (result.data) {
      commit('update', {
        pageCount: result.data[1],
        emailList: result.data[0]
      })
    }
  },
  async Deleteemail({ commit }, id) {
    const result = await Deleteemail(id)
    if (result) {
      Message.success('操作成功')
      const result = await getEmailPage()
      if (result.data) {
        commit('update', {
          pageCount: result.data[1],
          emailList: result.data[0]
        })
      }
    }
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
